const {con} = require('../db/db');

async function getUserList(page) {
    const sql = `select * from user order by id limit ${(page-1)*10},10;`;

    const total = `select * from user;`;

    let [rows] = await con.execute(sql);

    let [totals] = await con.execute(total);

    return {data:rows,total:totals.length}
}

async function addUsers(name,telephone,email,address) {
    const sql = `insert into user(name,telephone,email,address) values("${name}","${telephone}","${email}","${address}");`;

    await con.execute(sql);
}

async function editUsers(name,telephone,email,address,id) {
    const sql = `update user set name="${name}",telephone="${telephone}",email="${email}",address="${address}" where id=${id};`;

    await con.execute(sql);
}

async function delUsersById(id) {
    const sql = `delete from user where id=${id};`;

    await con.execute(sql);
}

module.exports={
    getUserList,
    addUsers,
    editUsers,
    delUsersById
}